package com.kcwl.idea.plugins.gateway.handler;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.kcwl.idea.plugins.gateway.config.KcGatewaySettings;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.*;

public class RequestApi {
    /**
     * 获取接口关联的资源列表
     *
     * @param productId
     * @param path
     * @return
     */
    public static List<String> listResourceByApi(Integer productId, String path) {
        Map<Object, Object> params = MapUtil.newHashMap();
        params.put("productType", productId);
        params.put("resourceUrl", path);
        String result = postBody("/resourcesList/detail", params);
        ResponseMessage<List<List<String>>> responseMessage = new Gson().fromJson(result, new TypeToken<ResponseMessage<List<List<String>>>>() {
        }.getType());
        List<List<String>> resultList = responseMessage.getResult();
        Set<String> resourceSet = new HashSet<>();
        for (List<String> resources : resultList) {
            resourceSet.addAll(resources);
        }
        return new ArrayList<>(resourceSet);
    }



    /**
     * 获取资源列表
     *
     * @return
     */
    public static Map<String, List<Menu>> resourcesList() {
        String result = post("resourcesList", MapUtil.newHashMap());
        ResponseMessage<Map<String, List<Menu>>> responseMessage = new Gson().fromJson(result, new TypeToken<ResponseMessage<Map<String, List<Menu>>>>() {
        }.getType());
        return responseMessage.getResult();
    }


    /**
     * 查询产品列表
     *
     * @return
     */
    public static List<KcProduct> listKcProduct() {
        String result = post("/productTypeList", new HashMap<>());
        ResponseMessage<List<KcProduct>> responseMessage = new Gson().fromJson(result, new TypeToken<ResponseMessage<List<KcProduct>>>() {
        }.getType());
        return responseMessage.getResult();
    }

    /**
     * 查询服务列表
     *
     * @return
     */
    public static List<KcService> listKcService() {
        String result = post("/serviceList", new HashMap<>());
        ResponseMessage<List<KcService>> responseMessage = new Gson().fromJson(result, new TypeToken<ResponseMessage<List<KcService>>>() {
        }.getType());
        return responseMessage.getResult();
    }

    public static ApiInfoPage pageApiInfo(String apiPath, String serviceName) {
        if (StrUtil.isBlank(serviceName)) {
            return new ApiInfoPage();
        }
        HashMap<String, Object> params = new HashMap<>();
        params.put("apiPath", apiPath);
        params.put("serviceName", serviceName);
        String result = postBody("/apiPage", params);
        return new Gson().fromJson(result, new TypeToken<ApiInfoPage>() {
        }.getType());
    }

    public static void addApi(ApiInfo apiInfo) {
        HttpUtil.post(KcGatewaySettings.getInstance().getGatewayUrl() + "/addApi", new Gson().toJson(apiInfo));
    }
    public static void editApi(ApiInfo apiInfo) {
        HttpUtil.post(KcGatewaySettings.getInstance().getGatewayUrl() + "/editApi", new Gson().toJson(apiInfo));
    }

    public static void flushCache() {
        HttpUtil.post(KcGatewaySettings.getInstance().getGatewayUrl() + "/refreshApi", new HashMap<>());
    }

    public static void delApi(Map<String, Object> params) {
        HttpUtil.post(KcGatewaySettings.getInstance().getGatewayUrl() + "/delApi", new Gson().toJson(params));
    }

    private static String post(String path, Map params) {
        return HttpUtil.post(KcGatewaySettings.getInstance().getGatewayUrl() + "/" + path, params);
    }

    private static String postBody(String path, Map params) {
        String jsonParams = JSONObject.toJSONString(params);
        return HttpUtil.post(KcGatewaySettings.getInstance().getGatewayUrl() + "/" + path, jsonParams);
    }

}



